import axios from 'axios';
// import store from '@/store';
import {Toast} from '@femessage/vant';
import qs from 'qs'
// create an axios instance
const service = axios.create({
    //自定义配置
    // baseURL: 'https://engini.cloud/payserver', // url = base api url + request url
    baseURL: 'https://engini.cn/payserver', // url = base api url + request url
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 10 * 1000 // request timeout 10秒 超时
});

// request拦截器 request interceptor
service.interceptors.request.use(
    (config) => {
        // 不传递默认开启loading
        if (!config.hideloading) {
            // loading
            Toast.loading({
                forbidClick: true
            });
        }
        if (config.form === true) {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
            config.data = qs.stringify(config.data) // 转为formdata数据格式
        }
        return config;
    },
    (error) => {
        // do something with request error
        console.log(error); // for debug
        return Promise.reject(error);
    }
);
// respone拦截器
service.interceptors.response.use(
    (response) => {
        Toast.clear();
        const res = response.data;
        if (res.status && res.status !== 200) {
            // 登录超时,重新登录
            if (res.status === 401) {
                // store.dispatch('FedLogOut').then(() => {
                //     location.reload();
                // });
            }
            return Promise.reject(res || 'error');
        } else {
            return Promise.resolve(res);
        }
    },
    (error) => {
        Toast.clear();
        console.log('err' + error); // for debug
        return Promise.reject(error);
    }
);

export default service;
